package com.terios.logic.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.terios.R;
import com.terios.Status;
import com.terios.logic.ui.imaCache.Anseylodar;
import com.terios.logic.ui.util.ViewHolder;

public class WeiboAdapter extends BaseAdapter {
	public List<Status> status;// 微博信息
	Context mContext;// 上下文
	Anseylodar ansylodar;

	public WeiboAdapter(Context context, List<Status> allstatus) {
		status = allstatus;
		mContext = context;
		ansylodar = new Anseylodar();
	}

	@Override
	public int getCount() {
		// 由于我们最后一项时更多 所以这里getcount+1
		return status.size();
	}

	@Override
	public Object getItem(int arg0) {
		return status.get(arg0);
	}

	// 请求更多的数据
	public void addmoreDate(List<Status> addmore) {
		if (status != null) {
			this.status.addAll(addmore);// 把新传得数据加到现在的list中
			this.notifyDataSetChanged();// 将数据追加到ListView中显示
		}
	}

	@Override
	public long getItemId(int index) {
		return status.get(index).getId();
	}

	@SuppressWarnings("static-access")
	@Override
	public View getView(int position, View conterView, ViewGroup arg2) {

		View statusView = null;
		if ((conterView != null) && (conterView.findViewById(R.id.contentPic) != null)) {
			statusView = conterView;
		} else {// 如果缓存中没有就重新创建
			statusView = LayoutInflater.from(mContext).inflate(R.layout.itemview, null);
		}
		ViewHolder vHolder = new ViewHolder();
		vHolder.tvitemName = (TextView) statusView.findViewById(R.id.tvItemName);
		vHolder.tvContent = (TextView) statusView.findViewById(R.id.tvItemContent);
		vHolder.ivitemPorprait = (ImageView) statusView.findViewById(R.id.ivItemPortrait);
		vHolder.ivitemPorprait.setBackgroundResource(R.drawable.portrait);// 重置头像为默认头像

		vHolder.tvitemTimeData = (TextView) statusView.findViewById(R.id.tvItemDate);

		vHolder.subLayoutView = (LinearLayout) statusView.findViewById(R.id.subLayout);
		vHolder.tvitemSubcontent = (TextView) statusView.findViewById(R.id.tvItemSubContent);
		
		vHolder.contentPic = (ImageView) statusView.findViewById(R.id.contentPic);
		vHolder.contentPic.setBackgroundResource(R.drawable.pic_bg);// 重置imageview中的内容
		vHolder.subContenPic = (ImageView) statusView.findViewById(R.id.subContentPic);
		vHolder.subContenPic.setBackgroundResource(R.drawable.preview_pic_loading);// 重置imageview中的内容
		
		Status mstatus = status.get(position);
		// 由于每个status的现实使用的都是共用一个itemview.xml模板，所以如果上一个用户是认证用户，就会把该模板中的认证图标设置为
		// 显示状态，若用户是非认证用户就必须重新设置该图标为隐藏
		if (mstatus.getUser().isVerified()) {
			vHolder.ivitemV = (ImageView) statusView.findViewById(R.id.ivItemV);
			vHolder.ivitemV.setVisibility(View.VISIBLE);// 是新浪认证用户，设置认证图标可见
		} else {
			vHolder.ivitemV = (ImageView) statusView.findViewById(R.id.ivItemV);
			vHolder.ivitemV.setVisibility(View.GONE);// 是新浪认证用户，隐藏认证图标
		}
		// 设定发表微博的用户的昵称
		vHolder.tvitemName.setText(mstatus.getUser().getName());
		// 设定内容
		vHolder.tvContent.setText(mstatus.getText());
		// 设定发表微博的时间
		vHolder.tvitemTimeData.setText(mstatus.getCreatedAt().toString().substring(11, 20));
		// 加载用户头像
		String usericon = mstatus.getUser().getProfileImageUrl();
		ansylodar.showimgAnsy(vHolder.ivitemPorprait, usericon);

		Status comentsStatus = status.get(position).getRetweeted_status();// 获取有转发内容;

		if (null != comentsStatus) {// 有转发
			if (comentsStatus.getDeleted() == 0) {// 被转发微博未删除
				vHolder.contentPic.setVisibility(View.GONE);
				vHolder.subLayoutView.setVisibility(View.VISIBLE);// 设置有转发布局可见
				vHolder.tvitemSubcontent.setText("@" + comentsStatus.getUser().getName() + ":" + comentsStatus.getText());
				String subconpic = comentsStatus.getThumbnail_pic();
				if (null != subconpic) {
					vHolder.subContenPic.setVisibility(View.VISIBLE);// 设置有转发图片可见
					ansylodar.showimgAnsy(vHolder.subContenPic, subconpic);
				}else{
					vHolder.subContenPic.setVisibility(View.GONE);// 隐藏有转发图片
				}
			} else {// 被转发微博已经删除
				vHolder.contentPic.setVisibility(View.GONE);
				vHolder.subContenPic.setVisibility(View.GONE);// 隐藏转发微博的图片view
				vHolder.subLayoutView.setVisibility(View.VISIBLE);// 设置有转发布局可见
				vHolder.tvitemSubcontent.setText(comentsStatus.getText());
			}
		} else {// 没有转发
			vHolder.subLayoutView.setVisibility(View.GONE);// 隐藏转发布局
			String pic_path = mstatus.getThumbnail_pic();
			if (null != pic_path) {
				vHolder.contentPic.setVisibility(View.VISIBLE);
				ansylodar.showimgAnsy(vHolder.contentPic, pic_path);
			}
		}
		return statusView;
	}

}
